<template>
  <div
    class="report-list-container"
    ref="reportListPage"
    v-loading.fullscreen.lock="loadingListData"
  >
    <div class="report-list-title-group-container">
      <div class="title">人员财务信息统计</div>
      <div>可按月统计每个人员的工资餐补等信息。</div>
    </div>
    <!--搜索-->
    <div class="report-list-search-group-container">
      <form class="base-search" onsubmit="return false;">
        <div class="report-list-base-search-group">
          <el-input v-model="params.keyword" placeholder="请输入报表名称" v-trim:blur>
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
          <base-button
            type="primary"
            @event="params.pageNum=1;search();trackEventHandler('search')"
            native-type="submit"
          >搜索</base-button>
          <base-button type="ghost" @event="resetParams">重置</base-button>
        </div>
      </form>
    </div>

    <!--list start-->
    <div class="report-list-component">
      <div class="operation-bar-container">
        <div class="reminder">历史生成的人员财务信息统计报表</div>
        <div class="top-btn-group" v-if="createdPermission">
          <base-button type="primary" @event="createReport(false)">创建</base-button>
        </div>
      </div>
      <el-table
        :data="reports"
        stripe
        :highlight-current-row="false"
        header-row-class-name="report-table-header"
        ref="multipleTable"
        class="report-table"
      >
        <el-table-column type="index" width="68" align="center" label="序号" :index="indexMethod"></el-table-column>
        <el-table-column
          v-for="(column, index) in columns"
          v-if="column.show"
          :key="`${column.field}_${index}`"
          :label="column.label"
          :prop="column.field"
          :width="column.width"
          :min-width="column.minWidth || '120px'"
          :class-name="column.field == 'reportName' ? 'report-name-superscript-td' : ''"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <template v-if="column.field === 'reportName'">
              <a href class="view-detail-btn" @click.stop.prevent="createReport(scope.row.id)">
                <pre class="pre-text">{{ scope.row[column.field] }}</pre>
              </a>
            </template>
            <template v-else-if="column.field=='createTime'">
              <span>{{scope.row.createTime | fmt_datetime}}</span>
            </template>
            <template v-else>
              <span>{{scope.row[column.field]}}</span>
            </template>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-footer">
        <div class="list-info">
          共
          <span class="level-padding">{{totalItems}}</span>记录
        </div>
        <el-pagination
          class="report-table-pagination"
          background
          @current-change="jump"
          @size-change="handleSizeChange"
          :page-sizes="defaultTableDatadefaultPageSizes"
          :page-size="params.pageSize"
          :current-page="params.pageNum"
          layout="prev, pager, next, sizes, jumper"
          :total="totalItems"
        ></el-pagination>
      </div>
    </div>
    <!--list end-->
  </div>
</template>

<script>
import _ from 'lodash';
import {getStatisticsReport} from '@src/api/userReportCustom.ts';
import { openAccurateTab } from '@src/util/platform'
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
import { defaultTableData } from '@src/util/table'
export default {
  name: 'finance-list-view',
  props: {
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      defaultTableData,
      // self state
      pending: false,
      loadingListData: false,
      params: {
        keyword: '',
        pageNum: 1,
        pageSize: 10
      },
      totalItems: 0,
      // data from remote
      reports: [],
      columns: this.fixedColumns(),
    };
  },
  computed: {
    createdPermission() {
      return this.initData.createFinance ;
    },
  },
  async mounted() {

    this.search();

  },
  methods: {
    indexMethod(index){
      return this.params.pageSize * (this.params.pageNum - 1) + index + 1
    },
    createReport(id){
      window.TDAPP.onEvent('pc：人员财务信息统计-新增人员信息');

      // window.location = '/customer/create';
      let fromId = window.frameElement.getAttribute('id');

      openAccurateTab({
        type: PageRoutesTypeEnum[id ? 'PageStaffPersonFinanceReportEdit' : 'PageStaffPersonFinanceReportCreate'],
        key: id,
        reload: true,
        fromId
      });
    },  
    search() {
      const params = this.buildParams();

      this.loadingListData = true;

      return getStatisticsReport(params)
        .then(res => {
          if (!res.data || !res.data.list) {
            this.reports = [];
            this.totalItems = 0;
            this.params.pageNum = 1;
          } else {
            const { total, pageNum, list } = res.data;

            this.reports = list
            this.totalItems = total;
            this.params.pageNum = pageNum;
          }

          return res;
        })
        .then(() => {
          this.$refs.reportListPage.scrollTop = 0;
          this.loadingListData = false;
        })
        .catch(err => {
          this.loadingListData = false;
          console.error('err', err);
        });
    },
    buildParams() {
      const sm = Object.assign({}, this.params);
      let params = {
        keywords: sm.keyword,
        pageSize: sm.pageSize,
        pageNum: sm.pageNum
      };
      return params;
    },
    jump(pageNum) {
      this.params.pageNum = pageNum;
      this.search();
    },
    handleSizeChange(pageSize) {
      this.saveDataToStorage('pageSize', pageSize);
      this.params.pageNum = 1;
      this.params.pageSize = pageSize;
      this.search();
    },
    getLocalStorageData() {
      const dataStr = localStorage.getItem('pageSize') || '{}';
      return JSON.parse(dataStr);
    },
    saveDataToStorage(key, value) {
      const data = this.getLocalStorageData();
      data[key] = value;
      localStorage.setItem('pageSize', JSON.stringify(data));
    },
    resetParams() {
      window.TDAPP.onEvent('pc：人员财务信息统计-重置事件');

      this.params = {
        keyword: '',
        pageNum: 1,
        pageSize: this.params.pageSize,
      };

      this.search();
    },
    fixedColumns() {
      return [
        {
          label: '报表名称',
          field: 'reportName',
          show: true,
          minWidth: '150px'
        },
        {
          label: '报表描述',
          field: 'reportDescribe',
          show: true
        },
        {
          label: '统计时间',
          field: 'statisticsDate',
          show: true
        },
        {
          label: '创建人',
          field: 'createUser',
          show: true
        },
        {
          label: '创建时间',
          field: 'createTime',
          show: true,
          width: '180px'
        },
      ];
    },
    // TalkingData事件埋点
    trackEventHandler(type) {
      if (type === 'search') {
        window.TDAPP.onEvent('pc：人员财务信息统计-搜索事件');
        return;
      }
    },
   
  },
};
</script>

<style lang="scss">

html,
body {
  height: 100%;
}

.level-padding {
  padding: 0 5px;
}

.advanced-search-linkman {
  .el-select-dropdown__item {
    height: 50px;
    padding: 5px 20px;
    p {
      margin: 0;
      line-height: 20px;
    }
  }
}

.report-advance-setting .el-dropdown-menu__item {
  width: 80px;
  text-align: center;
}

.report-list-container {
  height: 100%;
  overflow: auto;
  padding: 10px;

  .panel-title {
    font-size: 16px;
    line-height: 60px;
    padding: 0 25px;
    color: rgb(132, 138, 147);
    border-bottom: 1px solid rgb(242, 248, 247);
    font-weight: normal;
    display: flex;
    justify-content: space-between;
    .iconfont:hover {
      cursor: pointer;
    }
  }
}

.report-list-title-group-container{
  margin-bottom: 10px;
  background-color: #fff;
  border-radius: 3px;
  padding: 12px 10px;
  .title{
    font-weight: 600;
    font-size:16px ;
    margin-bottom: 5px;
  }
}

// search
.report-list-search-group-container {
  .advanced-search-function,
  .base-search {
    background: #fff;
    border-radius: 3px;
  }

  .base-search {
    font-size: 14px;
    display: flex;
    justify-content: space-between;
    padding: 12px 10px;

    .report-list-base-search-group {
      display: flex;
      min-width: 440px;
      justify-content: space-between;

     .el-input {
        width: 300px;
        input {
          height: 34px;
          line-height: 34px;
          width: 300px;
        }
      }

      a {
        line-height: 33px;
      }
    }

    .advanced-search-visible-btn {
      font-size: 14px;
      line-height: 32px;
      color: $color-primary;
      border-color: $color-primary;
      background: #fff;
      padding: 0 13px;
      white-space: nowrap;

      &:hover {
        cursor: pointer;
      }

      .iconfont {
        font-size: 12px;
        font-weight: bolder;
      }
    }
  }

  .advanced-search-form {
    overflow: auto;
    padding: 10px 0 63px 0;
    height: calc(100% - 51px);

    .form-item-container {
    }

    .two-columns {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        width: 50%;
      }
    }

    .el-form-item {
      .el-form-item__content,
      .el-select,
      .base-dist-picker,
      .el-cascader,
      .el-date-editor {
        width: 290px;
      }
    }

    .advanced-search-btn-group {
      display: flex;
      justify-content: flex-end;
      width: 100%;
      position: absolute;
      bottom: 0px;
      background: #fff;
      padding: 15px 20px;

      .base-button {
        margin: 0 10px;
      }
    }
  }

  .advanced-search-function {
    margin-top: 10px;
    padding-bottom: 10px;

    h4 {
      border-bottom: 1px solid #f4f4f4;
      padding: 10px;
    }

    .el-row {
      padding: 5px 0;
    }
    .input-label {
      text-align: right;
      line-height: 32px;
      padding-right: 0;
    }
  }
}

// list
.report-list-component {
  padding-top: 10px;
  /*min-height: calc(100% - 100px);*/

  .report-table {
    padding: 10px;

    &:before {
      height: 0;
    }

    .report-table-header th {
      background: #f5f5f5;
      color: $text-color-primary;
      font-weight: normal;
    }

      .view-detail-btn {
        width: 100%;
        color: $color-primary;
      }

    .view-detail-btn {
      color: $color-primary;
    }

    .select-column .el-checkbox {
      position: relative;
      top: 3px;
    }
  }

  .table-footer {
    display: flex;
    justify-content: space-between;
    padding: 0px 10px 10px 10px;
    background: #fff;
    border-radius: 0 0 3px 3px;

    .list-info {
      font-size: 13px;
      line-height: 32px;
      margin: 0;
      color: #767e89;

      .iconfont {
        position: relative;
        top: 1px;
      }
    }

    .el-pagination__jump {
      margin-left: 0;
    }
  }
}

.report-panel-btn {
  float: right;
  cursor: pointer;
  font-size: 14px;
  margin-right: 5px;

  &:hover {
    color: $color-primary;
  }
}

// -------- report selected panel --------
.report-selected-count {
  color: $color-primary;
  padding: 0 3px;
  width: 15px;
  text-align: center;
  cursor: pointer;
  font-size: 13px;
}

.report-selected-panel {
  font-size: 14px;
  height: calc(100% - 51px);

  .report-selected-tip {
    padding-top: 80px;

    img {
      display: block;
      width: 240px;
      margin: 0 auto;
    }

    p {
      text-align: center;
      color: #9a9a9a;
      margin: 30px 0 0 0;
      line-height: 20px;
    }
  }

  .report-selected-list {
    height: 100%;
    padding: 10px;
    overflow-y: auto;

    .report-selected-row {
      display: flex;
      flex-flow: row nowrap;
      line-height: 36px;
      border-bottom: 1px solid #ebeef5;
      font-size: 13px;

      &:hover {
        background-color: #f5f7fa;

        .report-selected-delete {
          visibility: visible;
        }
      }
    }

    .report-selected-head {
      background-color: #f0f5f5;
      color: #333;
      font-size: 14px;
    }

    .report-selected-sn {
      padding-left: 10px;
      width: 150px;
      @include text-ellipsis;
    }

    .report-selected-name {
      padding-left: 10px;
      flex: 1;
      @include text-ellipsis;
    }

    .report-selected-delete {
      width: 36px;
    }

    .report-selected-row button.report-selected-delete {
      padding: 0;
      width: 36px;
      height: 36px;
      border: none;
      background-color: transparent;
      outline: none;
      color: #646b78;
      visibility: hidden;

      i {
        font-size: 14px;
      }

      &:hover {
        color: #e84040;
      }
    }
  }
}

// operation
.report-columns-dropdown-menu {
  max-height: 300px;
  overflow: auto;
  .el-dropdown-menu__item {
    padding: 0;
  }
  .el-checkbox {
    width: 100%;
    padding: 5px 15px;
    margin: 0;
  }
}

.operation-bar-container {
  background: #fff;
  border-radius: 3px 3px 0 0;
  display: flex;
  justify-content: space-between;
  padding: 10px;

  .reminder{
      display: flex;
      align-items: center;
  }

  .top-btn-group .base-button {
    margin-right: 5px;
  }

  .action-button-group {
    .base-button {
      margin-left: 5px;
    }
  }

  .el-dropdown-btn {
    padding: 0 15px;
    line-height: 32px;
    display: inline-block;
    background: $color-primary-light-9;
    color: $text-color-primary;
    outline: none;
    margin-left: 5px;
    .iconfont {
      margin-left: 5px;
      font-size: 12px;
    }

    &:hover {
      cursor: pointer;
      color: #fff;
      background: $color-primary;
    }
  }
}

// superscript
.report-name-superscript-td {
  padding: 0 !important;
  & > div {
    height: 43px;
    line-height: 43px !important;
    a {
      display: inline-block;
      height: 43px;
      line-height: 43px;
    }
  }
}
</style>
